commit: Support -F/--body-file, like git
authorColin Walters <walters@verbum.org>
Sat, 11 Feb 2017 16:24:15 +0000 (11:24 -0500)
committerAtomic Bot <atomic-devel@projectatomic.io>
Tue, 14 Feb 2017 14:15:08 +0000 (14:15 +0000)
This is more convenient to script for projects which haven't
yet made the leap to using the API.

Closes: https://github.com/ostreedev/ostree/issues/674
Closes: #681
Approved by: jlebon

src/ostree/ot-builtin-commit.c
tests/basic-test.sh

index d3b634f97ea97d73a492c2b4694d29b19dd72407..218fb70182971a9cb4efa2390840e711e2038143 100644 (file)
@@ -32,6 +32,7 @@
 
 static char *opt_subject;
 static char *opt_body;
+static char *opt_body_file;
 static gboolean opt_editor;
 static char *opt_parent;
 static gboolean opt_orphan;
@@ -74,6 +75,7 @@ static GOptionEntry options[] = {
   { "parent", 0, 0, G_OPTION_ARG_STRING, &opt_parent, "Parent ref, or \"none\"", "REF" },
   { "subject", 's', 0, G_OPTION_ARG_STRING, &opt_subject, "One line subject", "SUBJECT" },
   { "body", 'm', 0, G_OPTION_ARG_STRING, &opt_body, "Full description", "BODY" },
+  { "body-file", 'F', 0, G_OPTION_ARG_STRING, &opt_body_file, "Commit message from FILE path", "FILE" },
   { "editor", 'e', 0, G_OPTION_ARG_NONE, &opt_editor, "Use an editor to write the commit message", NULL },
   { "branch", 'b', 0, G_OPTION_ARG_STRING, &opt_branch, "Branch", "BRANCH" },
   { "orphan", 0, 0, G_OPTION_ARG_NONE, &opt_orphan, "Create a commit without writing a ref", NULL },
@@ -349,6 +351,7 @@ ostree_builtin_commit (int argc, char **argv, GCancellable *cancellable, GError
   OstreeRepoCommitModifier *modifier = NULL;
   OstreeRepoTransactionStats stats;
   struct CommitFilterData filter_data = { 0, };
+  g_autofree char *commit_body = NULL;
 
   context = g_option_context_new ("[PATH] - Commit a new revision");
 
@@ -441,9 +444,18 @@ ostree_builtin_commit (int argc, char **argv, GCancellable *cancellable, GError
 
   if (opt_editor)
     {
-      if (!commit_editor (repo, opt_branch, &opt_subject, &opt_body, cancellable, error))
+      if (!commit_editor (repo, opt_branch, &opt_subject, &commit_body, cancellable, error))
         goto out;
     }
+  else if (opt_body_file)
+    {
+      commit_body = glnx_file_get_contents_utf8_at (AT_FDCWD, opt_body_file, NULL,
+                                                    cancellable, error);
+      if (!commit_body)
+        goto out;
+    }
+  else if (opt_body)
+    commit_body = g_strdup (opt_body);
 
   if (!ostree_repo_prepare_transaction (repo, NULL, cancellable, error))
     goto out;
@@ -576,7 +588,7 @@ ostree_builtin_commit (int argc, char **argv, GCancellable *cancellable, GError
           timestamp = g_date_time_to_unix (now);
           g_date_time_unref (now);
 
-          if (!ostree_repo_write_commit (repo, parent, opt_subject, opt_body, metadata,
+          if (!ostree_repo_write_commit (repo, parent, opt_subject, commit_body, metadata,
                                          OSTREE_REPO_FILE (root),
                                          &commit_checksum, cancellable, error))
             goto out;
@@ -592,7 +604,7 @@ ostree_builtin_commit (int argc, char **argv, GCancellable *cancellable, GError
             }
           timestamp = ts.tv_sec;
 
-          if (!ostree_repo_write_commit_with_time (repo, parent, opt_subject, opt_body, metadata,
+          if (!ostree_repo_write_commit_with_time (repo, parent, opt_subject, commit_body, metadata,
                                                    OSTREE_REPO_FILE (root),
                                                    timestamp,
                                                    &commit_checksum, cancellable, error))
index 5cad8ab3c2dba2c655e0d03f594d3a78e7acf4cd..d930813823bc33061cd75a7d5b835f81c07cb386 100644 (file)
@@ -19,7 +19,7 @@
 
 set -euo pipefail
 
-echo "1..60"
+echo "1..61"
 
 $OSTREE checkout test2 checkout-test2
 echo "ok checkout"
@@ -115,6 +115,22 @@ omitted_rev=$($OSTREE commit -b test2-no-subject-2 --timestamp="2005-10-29 12:43
 assert_streq $empty_rev $omitted_rev
 echo "ok commit no subject"
 
+cd ${test_tmpdir}
+cat >commitmsg.txt <<EOF
+This is a long
+commit message.
+
+Build-Host: foo.example.com
+Crunchy-With-Extra-Ketchup: true
+EOF
+$OSTREE commit -b branch-with-commitmsg -F commitmsg.txt -s 'a message' $test_tmpdir/checkout-test2-4
+$OSTREE log branch-with-commitmsg > log.txt
+assert_file_has_content log.txt '^ *This is a long$'
+assert_file_has_content log.txt '^ *Build-Host:.*example.com$'
+assert_file_has_content log.txt '^ *Crunchy-With.*true$'
+$OSTREE refs --delete branch-with-commitmsg
+echo "ok commit body file"
+
 cd ${test_tmpdir}
 $OSTREE commit -b test2-custom-parent -s '' $test_tmpdir/checkout-test2-4
 $OSTREE commit -b test2-custom-parent -s '' $test_tmpdir/checkout-test2-4